import { message } from 'antd';
import {
  reduceemployeeList,
  reduceemployeeDetail,
  getProList,
  getProDetail,
  getFeedback,
  empTabNum,
  sendTask,
  orderDetails,
} from '@/services/sendordermanage';

export default {
  state: {
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    detail: {},
    proBaseList: {},
    proFbList: [],
    proListData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    detailsData: {},
    tabList: [
      // { itemName: '待发单', itemCode: '1' },
      { itemName: '已发单', itemCode: '2' },
      { itemName: '已接单', itemCode: '3' },
      { itemName: '已回单', itemCode: '4' },
      { itemName: '问题人员', itemCode: '5' },
      { itemName: '已取消', itemCode: '6' },
    ],
    activeKey: '2',
    expandForm: false,
    formFieldsValue: {},
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(reduceemployeeList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;

      yield put({
        type: 'getList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取问题人员列表
    *fetchProList({ payload }, { call, put }) {
      const response = yield call(getProList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getProList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getProList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取页签数量
    *fetchTabNum({ payload }, { call, put, select }) {
      const response = yield call(empTabNum, payload.formFieldsValue);
      let activeKey = yield select(state => state.reduceempsend.activeKey);
      const tabList = yield select(state => state.reduceempsend.tabList);
      if (response.success) {
        if (response.data && response.data.length) {
          const sortNumber = (a, b) => a.itemCode - b.itemCode;
          const tabCodeArr = response.data.sort(sortNumber);
          const isHasBadgeCurrentTab = tabCodeArr.some(item => `${item.tabHdl}` === activeKey); // 当前页签有没有查询到
          if (!isHasBadgeCurrentTab) {
            activeKey = `${tabCodeArr[0].tabHdl}`;
          }
          tabCodeArr.forEach(item => {
            if (`${item.tabHdl}` !== activeKey) {
              tabList[item.tabHdl - 2].isHas = true;
            }
          });
          yield put({
            type: 'updateModelData',
            payload: { activeKey, tabList },
          });
          const getListParams = {
            ...payload.formFieldsValue,
            pageArg: { currentPage: payload.currentPage, pageSize: payload.pageSize },
          };
          if (activeKey === '5') {
            if (payload.formFieldsValue) {
              yield put({
                type: 'updateModelData',
                payload: {
                  ...payload.formFieldsValue,
                },
              });
            }
            yield put({
              type: 'problempage/getProList',
              payload: {
                ...getListParams,
                busiType: '12',
              },
            });
          } else {
            yield put({
              type: 'fetchList',
              payload: { ...getListParams, hdlState: activeKey },
            });
          }
        } else {
          yield put({
            type: 'getList',
            payload: {
              data: {},
              currentPage: 1,
              pageSize: 10,
            },
          });
          if (activeKey === '5') {
            yield put({
              type: 'problempage/updateProList',
              payload: {
                data: [],
                currentPage: 1,
                pageSize: 10,
              },
            });
          }
        }
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 发单
    *sendTask({ payload }, { call }) {
      const response = yield call(sendTask, payload);
      return response;
    },
    // 获取详情
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(reduceemployeeDetail, payload);
      if (response.success) {
        yield put({
          type: 'getDetail',
          payload: response.data,
        });
      }
    },
    // 获取问题人员详情
    *fetchProDetailList({ payload }, { call, put }) {
      const response = yield call(getProDetail, payload);
      if (response.success) {
        yield put({
          type: 'getProDetail',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '查询失败：服务端未知错误');
      }
    },
    // 详情
    *feactorderDetail({ payload, callback = () => {} }, { call, put }) {
      const response = yield call(orderDetails, payload);

      const sortNumber = (a, b) => a.operLeafType - b.operLeafType;
      if (response.success) {
        if (response.data.execStatusDto && response.data.execStatusDto.leafList) {
          response.data.execStatusDto.leafList = response.data.execStatusDto.leafList.sort(
            sortNumber
          );
        }
        yield put({
          type: 'orderDetails',
          payload: response.data,
        });
      } else {
        yield put({
          type: 'orderDetails',
          payload: {},
        });
        message.warning(`${response.message || '服务端错误！'}`);
      }
      callback(response);
    },
    // 问题人员反馈
    *proFeedBack({ payload, callback }, { call }) {
      const response = yield call(getFeedback, payload);
      if (response.success) {
        if (callback) {
          callback(response);
        }
      } else {
        message.success('反馈失败！');
      }
    },
    // 问题人员反馈并取消标记
    *cancelProbFlag({ payload, callback = () => {} }, { call }) {
      const response = yield call(getFeedback, payload);
      callback(response);
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 获取问题人员列表
    getProList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        proListData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 发单增员 - 详情
    orderDetails(state, { payload }) {
      return {
        ...state,
        detailsData: payload,
      };
    },
    // 获取详情
    getDetail(state, { payload }) {
      return {
        ...state,
        detail: payload,
      };
    },
    // 问题人员详情
    getProDetail(state, { payload }) {
      return {
        ...state,
        proBaseList: payload,
        proFbList: payload.probFdbkVos,
      };
    },

    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 设置页签状态
    updateTabListQuery(state, { payload }) {
      const { tabList } = state;
      tabList.forEach((item, index) => {
        if (item.itemCode === payload.activeKey) {
          tabList[index].isHas = false;
        }
      });
      return {
        ...state,
        tabList,
        ...payload,
      };
    },
  },
};
